package com.scuvc.controller;

import com.scuvc.entity.UserInfo;
import com.scuvc.utils.ImageUtils;
import com.scuvc.utils.StaticUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Date;

public class RegisterView extends JFrame {

    public void register(){
        setTitle("注册会员");
        //设置窗体起始位置
        setLocation(400,0);
        //设置窗体大小
        setSize(570,720);
        //把图片添加到标签里
        ImageIcon img = new ImageIcon("src/images/bg_addCar.jpg");
        ImageIcon bg = ImageUtils.createAlphaIcon(img,0.5f, getWidth(),getHeight());
        JLabel label = new JLabel(bg);
        //设置标签的大小
        label.setSize(getWidth(),getHeight());
        //把标签放在分层面板的最底层
        getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //把窗口面板设为内容面板并设为透明，流动布局
        JPanel pan = (JPanel)getContentPane();
        pan.setOpaque(false);
        pan.setLayout(new BorderLayout());
        //设置标题栏
        setTop(pan);

        JPanel panCenter = new JPanel();
        panCenter.setOpaque(false);
        GridLayout grid = new GridLayout(9,4);
        grid.setVgap(20);
        grid.setHgap(10);
        panCenter.setLayout(grid);
        Font font = new Font("仿宋",Font.BOLD,24);

        JLabel j1L1 = new JLabel();
        JLabel j1L2 = new JLabel("账\u3000\u3000号：");
        j1L2.setFont(font);
        j1L2.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j1f3 = new JTextField();
        j1f3.setFont(font);
        JLabel j1L4 = new JLabel();

        JLabel j2L1 = new JLabel();
        JLabel j2L2 = new JLabel("密\u3000\u3000码：");
        j2L2.setFont(font);
        j2L2.setHorizontalAlignment(SwingConstants.CENTER);
        //查询获取所有品牌信息获取品牌名字的下拉框
        JPasswordField j2p3 = new JPasswordField();
        j2p3.setFont(font);
        JLabel j2L4 = new JLabel();

        JLabel j3L1 = new JLabel();
        JLabel j3L2 = new JLabel("确认密码：");
        j3L2.setFont(font);
        j3L2.setHorizontalAlignment(SwingConstants.CENTER);
        JPasswordField j3p3 = new JPasswordField();
        j3p3.setFont(font);
        JLabel j3L4 = new JLabel();

        JLabel j4L1 = new JLabel();
        JLabel j4L2 = new JLabel("性\u3000\u3000别：");
        j4L2.setFont(font);
        j4L2.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j4f3 = new JTextField();
        j4f3.setFont(font);
        JLabel j4L4 = new JLabel();

        JLabel j5L1 = new JLabel();
        JLabel j5L2 = new JLabel("生\u3000\u3000日：");
        j5L2.setFont(font);
        JTextField j5f3 = new JTextField("yyyy/mm/dd");
        j5f3.setFont(font);
        JLabel j5L4 = new JLabel();

        JLabel j6L1 = new JLabel();
        JLabel j6L2 = new JLabel("身份证号：");
        j6L2.setFont(font);
        j6L2.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j6f3 = new JTextField();
        j6f3.setFont(font);
        JLabel j6L4 = new JLabel();

        JLabel j7L1 = new JLabel();
        JLabel j7L2 = new JLabel("驾照编号：");
        j7L2.setFont(font);
        j7L2.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j7f3 = new JTextField();
        j7f3.setFont(font);
        JLabel j7L4 = new JLabel();

        JLabel j8L1 = new JLabel();
        JLabel j8L2 = new JLabel("联系电话：");
        j8L2.setFont(font);
        j8L2.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j8f3 = new JTextField();
        j8f3.setFont(font);
        JLabel j8L4 = new JLabel();

        JLabel j9L1 = new JLabel();
        JButton j9b2 = new JButton("注\u3000册");
        j9b2.setFont(font);
        JButton j9b3 = new JButton("重\u3000置");
        j9b3.setFont(font);
        JLabel j9L4 = new JLabel();
        panCenter.add(j1L1);
        panCenter.add(j1L2);
        panCenter.add(j1f3);
        panCenter.add(j1L4);

        panCenter.add(j2L1);
        panCenter.add(j2L2);
        panCenter.add(j2p3);
        panCenter.add(j2L4);

        panCenter.add(j3L1);
        panCenter.add(j3L2);
        panCenter.add(j3p3);
        panCenter.add(j3L4);

        panCenter.add(j4L1);
        panCenter.add(j4L2);
        panCenter.add(j4f3);
        panCenter.add(j4L4);

        panCenter.add(j5L1);
        panCenter.add(j5L2);
        panCenter.add(j5f3);
        panCenter.add(j5L4);

        panCenter.add(j6L1);
        panCenter.add(j6L2);
        panCenter.add(j6f3);
        panCenter.add(j6L4);

        panCenter.add(j7L1);
        panCenter.add(j7L2);
        panCenter.add(j7f3);
        panCenter.add(j7L4);

        panCenter.add(j8L1);
        panCenter.add(j8L2);
        panCenter.add(j8f3);
        panCenter.add(j8L4);

        panCenter.add(j9L1);
        panCenter.add(j9b2);
        panCenter.add(j9b3);
        panCenter.add(j9L4);

        pan.add(panCenter,BorderLayout.CENTER);
        //设置窗体大小是否可调整
        setResizable(false);
        //设置窗体是否可见
        setVisible(true);
        //单击关闭按钮执行的操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //密码栏的焦点事件
        j2p3.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                j2p3.setBackground(Color.BLACK);
                j2p3.setForeground(Color.WHITE);
            }
            @Override
            public void focusLost(FocusEvent e) {
                j2p3.setBackground(Color.WHITE);
                j2p3.setForeground(Color.BLACK);
                String password = new String(j2p3.getPassword());
                if(password.equals("")){
                    JOptionPane.showMessageDialog(null, "密码不能为空",
                            "提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        j3p3.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                j3p3.setBackground(Color.BLACK);
                j3p3.setForeground(Color.WHITE);
            }
            @Override
            public void focusLost(FocusEvent e) {
                j3p3.setBackground(Color.WHITE);
                j3p3.setForeground(Color.BLACK);
                String password = new String(j2p3.getPassword());
                String pwd = new String(j3p3.getPassword());
                if(!password.equals(pwd)){
                    JOptionPane.showMessageDialog(null, "两次密码不一致",
                            "提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        j5f3.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                j5f3.setText("");
            }
            @Override
            public void focusLost(FocusEvent e) {
                if(j5f3.getText().equals("")){
                    j5f3.setText("yyyy/MM/dd");
                }
            }
        });
        //点击事件
        j9b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO 获取前台注册用户的值并保存
            }
        });
        j9b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                j1f3.setText("");
                j2p3.setText("");
                j3p3.setText("");
                j4f3.setText("");
                j5f3.setText("");
                j6f3.setText("");
                j7f3.setText("");
                j8f3.setText("");
            }
        });
        //账号栏焦点提示
        FocusListener focusListener = new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                j1f3.setBackground(Color.BLACK);
                j1f3.setForeground(Color.WHITE);
            }
            @Override
            public void focusLost(FocusEvent e) {
                j1f3.setBackground(Color.WHITE);
                j1f3.setForeground(Color.BLACK);
                String user_name = j1f3.getText();
                if(!user_name.equals("") && StaticUtils.userInfoService.getUser(user_name)){
                    JOptionPane.showMessageDialog(null, "该账户已存在",
                            "提示", JOptionPane.INFORMATION_MESSAGE);
                }
                if(user_name.equals("")){
                    JOptionPane.showMessageDialog(null, "账户不能为空",
                            "提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        };
        j1f3.addFocusListener(focusListener);
        StaticUtils.setFoucusListener(j4f3);
        StaticUtils.setFoucusListener(j5f3);
        StaticUtils.setFoucusListener(j6f3);
        StaticUtils.setFoucusListener(j7f3);
        StaticUtils.setFoucusListener(j8f3);
    }


    private void setTop(JPanel pan) {
        JPanel panTop = new JPanel();
        panTop.setLayout(new GridLayout(1,1));
        panTop.setOpaque(false);
        JLabel jTop = new JLabel("注册会员");
        jTop.setPreferredSize(new Dimension(
                jTop.getPreferredSize().width,100));
        jTop.setFont(new Font("仿宋",Font.BOLD,36));
        jTop.setHorizontalAlignment(SwingConstants.CENTER);
        panTop.add(jTop);
        pan.add(panTop,BorderLayout.NORTH);
    }
}

